import os
from collections import namedtuple
from utils import load_config, write_config
import pdb
Config = namedtuple("Config", ["arg", "dtype", "name"])

class ConfigParser:

    @classmethod
    def update_config(cls, src, menu, action, function, script, intr, configs):
        cfg = {menu: {action:dict(dict(function=function,
                        script=script, introduce=intr, configs=configs))}}
        if os.path.exists(src):
            try:
                config = load_config(src)
                assert config is not None
                for k1,v1 in cfg.items():
                    if not k1 in config:
                        config.update(cfg)
                    else:
                        for k2,v2 in v1.items():
                            config[k1].update({k2:v2})
                cfg = config
            except:
                print("更新异常.")
        write_config(cfg, src)

    @classmethod
    def delete_config(cls, src, menu, action):
        cfg = load_config(src)
        if not menu in cfg:
            return
        if not action in cfg[menu]:
            return
        del cfg[menu][action]
        write_config(cfg, src)

# if __name__ == "__main__":
#     cfgPath = ".config.json"
#     menu = "标注检查工具"
#     action = "统计数据集"
#     function = "skuCount"
#     script = "scripts/skuCount.py"
#     # intr = "给定前缀重命名数据集，项目类别_负责人名字缩写_时间（会自动在后面追加生成时间），如ty_ljj_20210121。如果数据集中包含Json文件，则会同时更新Json中图片路径信息。"
#     # intr = "检查拍照图片是否翻转90°（PIL和CV读取的宽高是否不一致）并修复。"
#     # intr =  "将已标注好labelme图片中的SKU按名称存储入相应的文件夹，以便数据检查，最后支持重新 生成Json标注。"
#     # intr = "在完成SKU文件夹检查纠错之后，重新生成Json文件。"
#     intr = "统计labelme标注数据集的图片数、SKU类别数和总框数。"
#     # gen_mask_from_crop(cropsrc, imgsrc, dst)
#     # crop_from_mask(src, dst, with_mask=False, exp_rate=0.2)
#     configs = [dict(arg='src', dtype='str', name='数据集路径*'),
#                dict(arg='dst', dtype='str', name='保存txt统计结果'),
#                ]

#     ConfigParser.update_config(cfgPath, menu, action, function, script, intr, configs)

#     # delete
#     #ConfigParser.delete_config(cfgPath, menu, action)
